<?php
/**
 * 将部门数组转换为树形结构
 * @param array $departments 部门数组
 * @param int $pid 父部门ID，默认为0（顶级部门）
 * @param string $idKey 部门ID键名，默认为'id'
 * @param string $pidKey 父部门ID键名，默认为'pid'
 * @return array 树形结构数组
 */
function arrayToTree(array $departments, $pid = 0, $idKey = 'id', $pidKey = 'pid'): array
{
    $tree = [];

    foreach ($departments as $department) {
        if ($department[$pidKey] == $pid) {
            // 查找当前部门的子部门
            $children = arrayToTree($departments, $department[$idKey], $idKey, $pidKey);
            if (!empty($children)) {
                $department['children'] = $children;
            }
            $tree[] = $department;
        }
    }

    return $tree;
}

/** 获取当前数据的子类
 * @param int $company_id
 */
function userIds($company_id)
{
    $userInfo = \app\model\admin\UserModel::where('id' , $company_id)->value('pid');//当前用户的部门PID
    //查找当前用户的子类部门id
    $pidList =\app\model\admin\DepartmentModel::where('pid',$userInfo)->field('id')->select()->toArray();
//    var_dump($pidList);exit;
    $res = '';
    foreach ($pidList as $pidList_1) {
        $res .=$pidList_1['id'].',';
    }
//    echo $res;exit;
   $res = rtrim($res, ',');
    if(empty($res)){
        return (string) $company_id;
    }else{
        $userListIds = \app\model\admin\UserModel::where('pid' ,'in', $res)->field('id')->select()->toArray();
        $resUserIDs = '';
        foreach ($userListIds as $userListIds) {
            $resUserIDs .=$userListIds['id'].',';
        }
        $resUserIDs = rtrim($resUserIDs, ',');
        if(empty($resUserIDs)){
            return (string) $company_id;
        }else{
            return $company_id.','.$resUserIDs;
        }
    }


//    return $userListIds;
}

function idCardMask($idCard)
{
    if (!is_string($idCard) || strlen($idCard) != 18 || !preg_match('/^\d{17}[\dXx]$/', $idCard)) {
        return '************';
    }

    return substr($idCard, 0, 6) . '********' . substr($idCard, 14);
}

/**
 * 根据父ID查找所有子分类
 * @param array $categories - 分类数组
 * @param int $parentId - 父分类ID
 * @return array 子分类数组
 */
function findChildren($categories, $parentId) {
    $result = [];

    foreach ($categories as $category) {
        if ($category['pid'] == $parentId) {
            // 找到直接子类
            $result[] = $category;

            // 递归查找子类的子类
            $children = findChildren($categories, $category['id']);
            $result = array_merge($result, $children);
        }
    }

    return $result;
}

/**
 * 获取指定ID的所有数据，包括其本身及其所有子分类
 * @param array $categories - 分类数组
 * @param int $id - 指定的分类ID
 * @return array 包含指定ID及其所有子分类的数据
 */
function getCategoryWithChildren($categories, $id) {
    $result = [];

    // 遍历数组找到指定ID的分类
    foreach ($categories as $category) {
        if ($category['id'] == $id) {
            $result[] = $category;
            break;
        }
    }

    // 如果找到了指定ID的分类，则继续查找其所有子分类
    if (!empty($result)) {
        $children = findChildren($categories, $id);
        $result = array_merge($result, $children);
    }

    return $result;
}

// 根据id获取父级路径
function getCategoryPath($categories, $id) {
    $path = [];

    while ($id > 0) {
        $found = false;

        foreach ($categories as $category) {
            if ($category['id'] === $id) {
                array_unshift($path, $category['department_name']);
                $id = $category['pid'];
                $found = true;
                break;
            }
        }

        // 如果没找到对应的 id，跳出循环防止死循环
        if (!$found) {
            break;
        }
    }

    return implode('/', $path);
}